﻿using System;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.Utility;
using System.Data;

namespace BCGC.BLL.Forum
{
    public class LatestPosts
    {
        private int postID;

        public int PostID
        {
            get { return postID; }
            private set { postID = value; }
        }
        private String userName;

        public String UserName
        {
            get { return userName; }
            private set { userName = value; }
        }
        private int userID;

        public int UserID
        {
            get { return userID; }
            private set { userID = value; }
        }
        private String postTitle;

        public String PostTitle
        {
            get { return postTitle; }
            private set { postTitle = value; }
        }
        private int totalReply;

        public int TotalReply
        {
            get { return totalReply; }
            private set { totalReply = value; }
        }
        private String latestPostUserName;

        public String LatestPostUserName
        {
            get { return latestPostUserName; }
            private set { latestPostUserName = value; }
        }
        private int latestPostUserID;

        public int LatestPostUserID
        {
            get { return latestPostUserID; }
            private set { latestPostUserID = value; }
        }

        private DateTime latestPostDate;

        public DateTime LatestPostDate
        {
            get { return latestPostDate; }
            private set { latestPostDate = value; }
        }

        internal LatestPosts(DataRow dataRow)
        {
            if (dataRow != null)
            {
                
                PostID = (int)CommonUtility.FilterNull(dataRow["PostID"], typeof(int));
                UserName = (string)CommonUtility.FilterNull(dataRow["UserName"], typeof(string));
                UserID = (int)CommonUtility.FilterNull(dataRow["UserID"], typeof(int));
                PostTitle = (string)CommonUtility.FilterNull(dataRow["PostTitle"], typeof(string));
                TotalReply = (int)CommonUtility.FilterNull(dataRow["TotalReply"], typeof(int));
                LatestPostUserName = (string)CommonUtility.FilterNull(dataRow["LatestPostUserName"], typeof(string));
                LatestPostUserID = (int)CommonUtility.FilterNull(dataRow["LatestPostUserID"], typeof(int));
                LatestPostDate = (DateTime)CommonUtility.FilterNull(dataRow["LatestPostDate"], typeof(DateTime));
                
            }
         }

        private static ReadWriteCustomCollection<LatestPosts> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<LatestPosts> collection = new ReadWriteCustomCollection<LatestPosts>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new LatestPosts(dr));
            }
            return collection;
        }

        internal static ReadWriteCustomCollection<LatestPosts> GetAllLatestPost(int categoryID, string sortColumn, string sortDirection, int currentPage, int pazeSize, out int totalCount)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostData.GetAllLatestPost(categoryID, sortColumn, sortDirection, currentPage, pazeSize, out totalCount);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }
       
    }
}
